package com.sky.module.system.file.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.common.domain.form.FileQueryForm;
import com.sky.module.suppot.file.domain.entity.FileEntity;
import com.sky.module.suppot.file.s3.OssProperties;
import com.sky.module.system.file.service.FileService;
import com.sky.module.system.user.domain.entity.User;
import com.sky.utils.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author
 * @date 2023/3/1 13:33
 **/
@RestController
@RequestMapping("/api/file")
public class FileController {

    @Resource
    private FileService fileService;

    @PostMapping("/list")
    public Result<?> list(@RequestBody FileQueryForm fileQueryForm) {
        final Page<FileEntity> page = fileService.queryPage(fileQueryForm);
        return Result.ok(page);
    }


    /***
     * @description 文件上传
     * @param file
     * @param module  比如 avatar  video
     * @return com.sky.utils.Result
     * @date 2023/3/2 1:57
     *
     */
    @PostMapping("upload")
    public Result upload(MultipartFile file, @RequestParam String module) {

        System.out.println("module = " + module);
        System.out.println("file.getContentType() = " + file.getContentType());
        final Result result = fileService.uploadFile(file, module);
        System.out.println("file.getOriginalFilename() = " + file.getOriginalFilename());
        return Result.ok(result).message("文件上传成功");
    }

    @PostMapping("delete")
    public Result delete(@RequestParam String fileKey) {
        fileService.delete(fileKey);
        return Result.ok().message("删除成功");


    }

    @GetMapping("getFileUrl")
    public Result getFileUrl(@RequestParam String fileKey) {
        final Result<?> fileUrl = fileService.getFileUrl(fileKey);
        return fileUrl;
    }
}
